package com.moyu.plugins.pokemsg;

import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.ProtocolManager;
import com.moyu.plugins.pokemsg.Command.PMsgCommandExecutor;
import com.moyu.plugins.pokemsg.Listener.CaptureListener;
import com.moyu.plugins.pokemsg.Listener.SpawnListener;
import com.pixelmonmod.pixelmon.entities.pixelmon.stats.IVStore;
import net.milkbowl.vault.chat.Chat;
import net.milkbowl.vault.economy.Economy;
import net.milkbowl.vault.permission.Permission;
import org.bukkit.Bukkit;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Random;


public final class PokeMsg extends JavaPlugin {

    private Permission permission = null;
    private Economy economy = null;
    private Chat chat = null;
    public CustomConfig biomeYml = new CustomConfig("biome");
    private static final Random rand = new Random();
    ProtocolManager pm;

    @Override
    public void onEnable() {
        //插件目录不存在时创建
        if (!getDataFolder().exists())
        {
            //noinspection ResultOfMethodCallIgnored
            getDataFolder().mkdir();
            getLogger().info("【精灵广播】工作目录创建成功~");
        }


		if (!initVault()){
		       getLogger().severe("初始化Vault支持失败,请检测是否已经安装Vault插件和权限插件、经济插件");
		       return;
		}


        if (Bukkit.getPluginManager().isPluginEnabled("ProtocolLib"))
        {
            pm = ProtocolLibrary.getProtocolManager();
            //pm.addPacketListener(this.getPacketAdapter(PacketType.Play.Server.BOSS));
            getLogger().info("正在使用 ProtocolLib 插件作为前置~~~");

        }
        else
        {
            getLogger().info("未找到 ProtocolLib 插件，启动失败。");
            getLogger().info("请安装 ProtocolLib 后重试！");
            Bukkit.getPluginManager().disablePlugin(this);
        }

        //判断file即config.yml是否存在
        this.saveDefaultConfig();	//不存在时将jar中自带的config拉到工作目录下
        biomeYml.saveDefaultConfig();
        this.reloadConfig();
        biomeYml.reloadConfig();

        //注册命令执行器
        this.getCommand("pmsg").setExecutor(new PMsgCommandExecutor(this));
        getLogger().info("插件命令已载入~~~");

        //注册监听器
        getServer().getPluginManager().registerEvents(new CaptureListener(this), this);
        getServer().getPluginManager().registerEvents(new SpawnListener(this), this);
        getLogger().info("插件监听器已载入~~~");

        getLogger().info("精灵广播系统加载完成~~~");
        getLogger().info("本插件由【墨鱼】制作完成！");
        getLogger().info("【墨鱼】联系QQ：1971911052");
    }

    @Override
    public void onDisable() {
        // Plugin shutdown logic
    }

    public ProtocolManager getPM() {
        return pm;
    }

    public Chat getChat()
    {
        return this.chat;
    }

    @SuppressWarnings("unused")
    public Economy geteEonomy()
    {
        return this.economy;
    }
    @SuppressWarnings("unused")
    public Permission getPermission()
    {
        return this.permission;
    }


    private boolean initVault(){
        boolean hasNull = false;
        //获取权限系统实例
        RegisteredServiceProvider<Permission> permissionProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.permission.Permission.class);
        if (permissionProvider != null) {
            if ((permission = permissionProvider.getProvider()) == null) hasNull = true;
        }
        //初始化聊天系统实例
        RegisteredServiceProvider<Chat> chatProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.chat.Chat.class);
        if (chatProvider != null) {
            if ((chat = chatProvider.getProvider()) == null) hasNull = true;
        }
        //初始化经济系统实例
        RegisteredServiceProvider<Economy> economyProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class);
        if (economyProvider != null) {
            if ((economy = economyProvider.getProvider()) == null) hasNull = true;
        }
        return !hasNull;
    }


    public int getPercentage(IVStore ivs) {
        double x =(ivs.attack +
                ivs.defence +
                ivs.hp +
                ivs.specialAttack +
                ivs.specialDefence +
                ivs.speed)/186.0;

        x = new BigDecimal(x).setScale(2, RoundingMode.HALF_UP).doubleValue()*100;

        return (int) x;

    }

    public <T extends Enum<T>> T random(Class<T> ec) {
        return random(ec.getEnumConstants());
    }
    public <T> T random(T[] values) {
        return values[rand.nextInt(values.length)];
    }

}
